import { Component, OnInit, ViewChild } from '@angular/core';
import { ColumnType, SPage, TablePageComponent } from '@shared/components/table-page/table-page.component';
import { Body, _HttpClient } from '@delon/theme';
import { NzMessageService, NzModalService } from 'ng-zorro-antd';
import { UpgradeEditComponent } from '../upgrade-edit/upgrade-edit.component';
import { UpgradeSelectComponent } from '../upgrade-select/upgrade-select.component';
import { ApiService } from '@core/services/api';
import { STData } from '@delon/abc';
import { from } from 'rxjs';
import { ACLService } from '@delon/acl';

@Component({
  selector: 'upgrade-list',
  templateUrl: './upgrade-list.component.html',
  styles: []
})
export class UpgradeListComponent implements OnInit {
  @ViewChild('st', { static: false }) st: TablePageComponent;
  recordFlag:boolean = this.aclService.canAbility("upgrade-manage/record");//检查用户按钮权限
  upgradeFlag:boolean =  this.aclService.canAbility("upgrade-manage/upgrade");
  selectData: any[];
  loading = false;
  addUserInfo = {
    userAccount: '',
    userPassword: '',
    userName: '',
    roles: [],
    depts: []
  };
  config: SPage = {
    columns: [
      {
        index: 'id',
        title: '多选',
        type: ColumnType.checkbox,
      },
      {
        index: 'deviceCode',
        title: '设备SN码',
        width:150,
        filter: {
          placeholder: '请输入设备SN码',
          type: 'string'
        }
      },
      {
        index: 'deviceDescribe',
        title: '设备描述',
        width:200,
        filter: {
          placeholder: '请输入设备描述',
          type: 'string'
        }
      },
      {
        index: 'deviceFac',
        title: '设备厂商',
        width:120,
        filter: {
          placeholder: '请输入厂商',
          type: 'string'
        }
      },
      {
        index:'deviceCategory',
        title:'设备类型',
        width:100,
        type: ColumnType.tag,
        tag: {
          1:{text: '大门口机'},
          2:{text: '管理机'},
          3:{text: '单元机'},
          4:{text:'室内机'}
        },
      },
      {
        index: 'deviceType',
        title: '设备分类',
        width:120,
        type: ColumnType.tag,
        tag:{
          1:{text: '门禁摄像机'},
          2:{text: '门禁刷卡'},
          3:{text: 'IPC'},
          4:{text: 'NVR'},
          5:{text: '微型门禁产品'},
          6:{text: '安卓门禁产品'},
          7:{text: '门铃'},
          8:{text: '4G开门控制器'},
          12:{text: 'linux人脸机'}
        },
        filter: {
          placeholder: '请选择设备分类',
          type: 'select',
          items: [
            {label: '门禁摄像机', value: 1},
            {label: '门禁刷卡', value: 2},
            {label: 'IPC', value: 3},
            {label: 'NVR', value: 4},
            {label: '微型门禁产品', value: 5},
            {label: '安卓门禁产品', value: 6},
            {label: '门铃', value: 7},
            {label: '4G开门控制器',value: 8},
            {label: 'linux人脸机',value: 12}
          ]
        }
      },
      {
        index: 'deviceLocation',
        title: '安装地址',
        width:200,
        filter: {
          placeholder: '请输入安装地址',
          type: 'string'
        },
        //className:'w200'
      },
      {
        index: 'deviceLocationStatus',
        title: '绑定状态',
        width:120,
        type: ColumnType.tag,
        tag: {
          0:{text: '未绑定',color: 'red'},
          1:{text: '已绑定',color: 'green'}
        },
        filter: {
          placeholder: '请选择绑定状态',
          type: 'select',
          items: [
            {label: '未绑定', value: 0},
            {label: '已绑定', value: 1},
          ]
        }
      },
      {
        index: 'deviceLastStatus',
        title: '设备状态',
        width:120,
        type: ColumnType.tag,
        sort:true,
        tag:{
          0:{text: '离线',color: 'red'},
          1:{text: '在线',color: 'green'}
        },
        filter: {
          placeholder: '请选择设备状态',
          type: 'select',
          items: [
            {label: '离线', value: 0},
            {label: '在线', value: 1},
          ]
        }
      },
      {
        index:'deviceAppVersion',
        title:'终端程序版本',
        width:150,
      },
      {
        index:'deviceMcuVersion',
        title:'MCU版本',
        width:150,
      },
      {
        index:'deviceReadHeadVersion',
        title:'读头版本',
        width:150,
      },
      {
        index:'deviceSystemVersion',
        title:'固件版本',
        width:150,
      },
      {
        index:'deviceIpc',
        title:'IPC版本',
        width:150,
      },
      {
        index: 'deviceIp',
        title: '门禁IP',
        width:150,
        filter:{
          placeholder:'请输入设备IP地址',
          type:'string'
        }
      },
      {
        index: 'status',
        title: '状态',
        width:150,
        type: ColumnType.tag,
        tag:{
          true:{text:'启用',color:'green'},
          false:{text:'停用',color:'red'}
        }
      },
      {
        index: '',
        title: '操作',
        width:120,
        fixed:'right',
        buttons:[
          {
            text:'升级记录',
            type:'modal',
            iif:()=>{
              return this.recordFlag;
            },
            click:(_record)=>{
              this.editDic(_record)
            }
          }
        ]
      },
    ],
    showCheckBox: false,
    reqUrl:'/core/host/page',
    // editBtn: [
    //   { title: '升级记录'}
    // ]
  }
  constructor(private http: _HttpClient,  private api: ApiService, private modal: NzModalService, private message: NzMessageService,private aclService: ACLService) { }
  
  ngOnInit() {

  }


  // 编辑功能开始
  editDic(_param?: object): void {
    const title = '升级记录';
    const modal = this.modal.create({
      nzTitle: title,
      nzContent: UpgradeEditComponent,
      nzWidth: 1000,
      nzComponentParams: {
        _param
      },
      nzFooter: null
    })
  }
  
  operate(e: any): void {
    if (e.title === '升级记录') {
      this.editDic( e.data);
    }
  }
  _console(e: any): void {
    this.selectData = e;
    console.log(e);
    if(e == undefined || e.length == 0){
      return;
    }
    let arr=[];
    e.forEach(element => {
     arr.push(element.deviceType); 
    });
    arr=arr.sort();
    for(let i=0;i<arr.length - 1;i++){
      if(arr[i] != arr[i+1]){
        this.message.error("请选择相同类型的设备!");
        return ;
      }
    }
   
  }
 
  dataProcess(data: STData[]){
    console.log(data);
    for(let item of data){
      item.deviceFac="兴天下";
    }
    return data;
  }
  //升级设备
  updateAll() {
    if (!!this.selectData && this.selectData.length !== 0) {

      let arr=[];
      this.selectData.forEach(element => {
       arr.push(element.deviceType); 
      });
      arr=arr.sort();
      for(let i=0;i<arr.length - 1;i++){
        if(arr[i] != arr[i+1]){
          this.message.error("请选择相同类型的设备!");
          return ;
        }
      }
      const title = '设备升级';
      const modal = this.modal.create({
        nzTitle: title,
        nzContent: UpgradeSelectComponent,
        nzWidth: 1000,
        nzComponentParams: {
          _param:this.selectData
        },
        nzOnOk: () => {
          this.st.reload();
        },
        nzFooter: null
      })
      modal.afterClose.subscribe(res => {
        if (res === 'ok') {
          this.st.reload();
        }
      })
    } else {
      this.message.warning('请选择设备');
    }
  }
  

}
